<template>
  <el-dialog :title="title" :visible.sync="isShow" :show-close="false" :close-on-click-modal="false" :close-on-press-escape="false" @open="openEvent">
    <el-form :model="form">
      <el-form-item label="应用名称" :label-width="formLabelWidth">
        <el-input v-model="form.applicationNameZh" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="开发工程名称" :label-width="formLabelWidth">
        <el-input v-model="form.applicationName" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="接口同步地址" :label-width="formLabelWidth">
        <el-input v-model="form.interfaceSyncUrl" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="备注" :label-width="formLabelWidth">
        <el-input v-model="form.remark" autocomplete="off"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="closeDialog">取 消</el-button>
      <el-button type="primary" @click="operateAppInfo">确 定</el-button>
    </div>
  </el-dialog>
</template>
  
<script>
import { addApplication, updateApplication } from '@/api/application'

export default {
  name: 'ApplicationInfoComp',
  data () {
    return {
      form: {
        applicationNameZh: '',
        applicationName: '',
        interfaceSyncUrl: '',
        remark: ""
      },
      formLabelWidth: '120px',
      title: '新增应用'
    }
  },
  props: {
    isShow: {
      type: Boolean,
      required: true
    },
    item: {
      type: Object,
      require: false
    }
  },
  methods: {
    openEvent () {
      if (this.item === undefined) {
        this.form.applicationNameZh = ""
        this.form.applicationName = ""
        this.form.interfaceSyncUrl = ""
        this.form.remark = ""
        this.title = '新增应用'
      } else {
        this.title = '编辑应用'
        this.form = { ...this.item }
      }
    },
    closeDialog () {
      this.$emit("changeDialogStatus", false);
    },
    result (data) {
      this.$emit("changeDialogStatus", "success");
      if (data.status === 200) {
        this.$message.success("操作成功");
      } else {
        this.$message.error("操作失败，请重试");
      }
    },
    operateAppInfo () {
      const param = { ...this.form }
      if (this.item === undefined) {
        addApplication(param).then((res) => {
          this.result(res)
        });
      } else {
        updateApplication(param).then((res) => {
          this.result(res)
        });
      }
    }
  }
}
</script>
  
<style scoped>
</style>
  